package de.koller.million.render2D;

import de.koller.million.render.util.BufferContainer;
import de.koller.million.render2D.eng.Screen2D;
import de.koller.million.render2D.szene.Scene;
import de.koller.oceanFramework.gl.util.InputGL;
import de.koller.oceanFramework.gl.util.ViewUtil;
import de.koller.oceanFramework.tree.Child;
import de.koller.oceanFramework.tree.Sea3X;
import org.lwjgl.input.Keyboard;

public class RenderRoot2D extends Sea3X {
	
	private RenderStatus renderStatus = RenderStatus.Default;
	
	@Child public final BufferContainer buffer = new BufferContainer();
	@Child public final	InputGL input = new InputGL();
	@Child public final Screen2D screen = new Screen2D();
	
	@Child public final Scene scene = new Scene();

	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		if( input.isKeyPressed( Keyboard.KEY_RETURN ) && renderStatus == RenderStatus.Default )
			renderStatus = RenderStatus.Base;
	}
	
	@Override
	protected void render( int elapsedTime ) {
		super.render( elapsedTime );
		
		if( renderStatus == RenderStatus.Light ) {
			ViewUtil.readPixels( screen.getScreenLayer() );
		} 
		if( renderStatus == RenderStatus.CameraLight ) {
			ViewUtil.readPixels( screen.getLightLayer() );
		}
		if( renderStatus == RenderStatus.Finish ) {
			screen.generateFrame();
			//@todo generate cameraLight
		}
		
		ViewUtil.initOrtho();
	}

	@Override
	public void postChildRender() {
		super.postChildRender();
		
		if(		 renderStatus == RenderStatus.Base )		renderStatus = RenderStatus.Light;
		else if( renderStatus == RenderStatus.Light )		renderStatus = RenderStatus.CameraLight;
		else if( renderStatus == RenderStatus.CameraLight ) renderStatus = RenderStatus.Finish;
		else if( renderStatus == RenderStatus.Finish )		renderStatus = RenderStatus.Default;	
	}
	
	public RenderStatus getRenderStatus() {
		return renderStatus;
	}
	
}
